package org.csu.mypetstore.api.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.csu.mypetstore.api.entity.Account;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponce<T> implements Serializable {

    @Setter
    @Getter
    private int status;
    private String msg;
    private T data;
    private Map<String, Object> extraData = new HashMap<>();

    private String key;
    private Object value;

    private CommonResponce(int status){
        this.status=status;
    }
    private CommonResponce(int status,String msg){
        this.status=status;
        this.msg=msg;
    }
    private CommonResponce(int status,T data,String msg){
        this.status=status;
        this.msg=msg;
        this.data=data;
    }
    private CommonResponce(int status,T data){
        this.status=status;
        this.data=data;
    }
    private CommonResponce(String key, Object value, T data){
        this.key=key;
        this.value=value;
        this.data=data;
    }
    private CommonResponce(int status,String msg,T data){
        this.status=status;
        this.msg=msg;
        this.data=data;
    }
    @JsonIgnore
    public boolean isSuccess(){
        return this.status==ResponseCode.SUCCESS.getCode();
    }
    //对应CommonResponce(int status)。。。。
    public static <T>CommonResponce<T> createForSuccess(){
        return  new CommonResponce<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T>CommonResponce<T>createForSuccess(T data){
        return new CommonResponce<T>(ResponseCode.SUCCESS.getCode(), data);
    }
    public static <T>CommonResponce<T>createForSuccessMessage(String msg){
        return  new CommonResponce<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
    public static <T>CommonResponce<T> createForSuccess(String msg,T data){
        return  new CommonResponce<T>(ResponseCode.SUCCESS.getCode(),data,msg);
    }

    //错误响应
    public static <T>CommonResponce<T> createForError(){
        return new CommonResponce<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDescription());
    }
    public static <T>CommonResponce<T>createForError(String msg){
        return new CommonResponce<T>(ResponseCode.ERROR.getCode(),msg);
    }
    public static <T>CommonResponce<T>createForError( int code,String msg){
        return new CommonResponce<T>(code,msg);
    }

//    public <T>CommonResponce<T>put(String key, Object value, T data){
//        if (data==null)
//        {
//            data= (T) new HashMap<>();
//        }
//        ((Map) data).put(key,value);
//        return (CommonResponce<T>) this;
//        ////////这里
//    }
//
    public CommonResponce<T> put(String key, Object value) {
    extraData.put(key, value);
    return this;
    }
    public static <T>CommonResponce<T>createForSuccessMessage(int code,String msg){
        return  new CommonResponce<T>(code,msg);
    }

    public static <T> CommonResponce<T> createForSuccessMessage(int code, String msg, T date) {
        return new CommonResponce<T>(code,msg,date);
    }

    public void setData(Account data) {
        this.data= (T) data;
    }
}
